package com.zf.yichat.controller.sys;


import com.zf.yichat.controller.BaseController;
import com.zf.yichat.model.SysUser;
import com.zf.yichat.model.SysUserRole;
import com.zf.yichat.service.SysUserService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsPage;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.internal.util.Contracts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

/**
 * @author fengsong
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private SysUserService userService;


    @RequestMapping("/index")
    public String index() {
        getRequest().setAttribute("user", getUser());
        return "sys/user/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit, String name) {

        return userService.selectIndexList(FsPage.init(page, limit), name);
    }

    /**
     * 新增后台管理用户
     * @param id
     * @return
     */
    @RequestMapping("save/index")
    public String saveIndex(Integer id) {
        // 查询所有角色，方便页面为用户绑定角色使用
        getRequest().setAttribute("roleMap", DtoChangeUtils.getMap(userService.selectRoleList(), SysUserRole::getId, SysUserRole::getName));

        // 用户回显用户数据
        if (Objects.nonNull(id)) {
            getRequest().setAttribute("user", userService.selectUserById(id));
        }
        return "sys/user/save";
    }

    /**
     * 新增后台用户
     * @param user
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public FsResponse save(SysUser user) {
        Contracts.assertTrue(Objects.nonNull(user.getId()) || Objects.isNull(userService.selectUserByUsername(user.getUsername())), "用户名已存在");
        user.setUserid(getUserId());
        return FsResponseGen.gen(userService.saveUser(user) == 1);
    }

    /**
     * 系统用户重置密码
     * @param id
     * @return
     */
    @RequestMapping("password/reset")
    @ResponseBody
    public FsResponse passwordReset(Integer id) {
        SysUser user = userService.selectUserById(id);
        if (StringUtils.isNotBlank(user.getUsername())) {
            SysUser sysUser = new SysUser();
            sysUser.setId(id);
            sysUser.setUsername(user.getUsername());
            sysUser.setPassword("000000");
            return FsResponseGen.gen(userService.passwordReset(sysUser) == 1);
        }
        return FsResponseGen.gen(false);
    }

    /**
     * 系统用户操作状态
     * @param id
     * @return
     */
    @RequestMapping("status/update")
    @ResponseBody
    public FsResponse statusUpdate(Integer id) {
        return FsResponseGen.gen(userService.updateUserStatus(id) == 1);
    }

    /**
     * 系统用户-删除操作
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public FsResponse delete(Integer id) {
        return FsResponseGen.gen(userService.delete(id) == 1);
    }


    @RequestMapping("/query/select")
    @ResponseBody
    public FsResponse forSelectList() {
        return null;
    }


}
